package jgl.android.recyclerviewrefresh.header;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import jgl.android.loadinglibrary.LoadingIndicatorView;
import jgl.android.loadinglibrary.indicators.BallPulseIndicator;
import jgl.android.recyclerviewrefresh.IHeaderView;
import jgl.android.recyclerviewrefresh.OnAnimEndListener;
import com.android.recyclerviewrefresh.R;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;

/**
 * Created by Administrator on 2018/9/3.
 */

public class CommonRefreshView extends FrameLayout implements IHeaderView {

    // private ImageView refreshArrow;
    //   private ImageView loadingView;
    private TextView refreshTextView;
    private View rootView;
    private View rlContent;
    private LoadingIndicatorView loadingView;

    public CommonRefreshView(Context context) {
        this(context, null);
    }

    public CommonRefreshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        rootView = View.inflate(getContext(), R.layout.view_common_header, null);
        // refreshArrow = (ImageView) rootView.findViewById(R.id.iv_arrow);
        loadingView = rootView.findViewById(R.id.loading);
        refreshTextView = rootView.findViewById(R.id.tv);
        rlContent = rootView.findViewById(R.id.rlContent);
        // loadingView = (ImageView) rootView.findViewById(R.id.iv_loading);
        loadingView.setIndicator(new BallPulseIndicator());
        loadingView.setIndicatorColor(getContext().getResources().getColor(R.color.gray_99));
        addView(rootView);
    }

    public void setArrowResource(@DrawableRes int resId) {
        // refreshArrow.setImageResource(resId);
    }  public void setRefreshTextViewVisibility( int visibility) {
        refreshTextView.setVisibility(visibility);
    }

    public void setRootViewBackgroundResource(int r) {
        rlContent.setBackgroundResource(r);
    }

    public void setTextColor(@ColorInt int color) {
        refreshTextView.setTextColor(color);
    }

    public void setPullDownStr(String pullDownStr1) {
        pullDownStr = pullDownStr1;
    }

    public void setReleaseRefreshStr(String releaseRefreshStr1) {
        releaseRefreshStr = releaseRefreshStr1;
    }

    public void setRefreshingStr(String refreshingStr1) {
        refreshingStr = refreshingStr1;
    }

    private String pullDownStr = "下拉刷新";
    private String releaseRefreshStr = "释放刷新";
    private String refreshingStr = "正在刷新";

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onPullingDown(float fraction, float maxHeadHeight, float headHeight) {
        if (fraction < 1f) refreshTextView.setText(pullDownStr);
        if (fraction > 1f) refreshTextView.setText(releaseRefreshStr);
        // refreshArrow.setRotation(fraction * headHeight / maxHeadHeight * 180);
    }

    @Override
    public void onPullReleasing(float fraction, float maxHeadHeight, float headHeight) {
        if (fraction < 1f) {
            refreshTextView.setText(pullDownStr);
           /* refreshArrow.setRotation(fraction * headHeight / maxHeadHeight * 180);
            if (refreshArrow.getVisibility() == GONE) {
                refreshArrow.setVisibility(VISIBLE);
                loadingView.setVisibility(GONE);
            }*/
        }
    }

    @Override
    public void startAnim(float maxHeadHeight, float headHeight) {
        refreshTextView.setText(refreshingStr);
        //   refreshArrow.setVisibility(GONE);
        // loadingView.setVisibility(VISIBLE);

    }

    @Override
    public void onFinish(OnAnimEndListener listener) {
        listener.onAnimEnd();
    }

    @Override
    public void reset() {
        //  refreshArrow.setVisibility(VISIBLE);
        //  loadingView.setVisibility(GONE);
        refreshTextView.setText(pullDownStr);
    }
}
